package com.address;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 正则表达式获得地址信息
 */
public class GetAddress {

    public static void main(String[] args) {
        //1.中华人民共和国吉林省长春市二道区临河街万兴小区4栋2门
        //2.中华人民共和国河北省石家庄市武林区砂石街河东小区8栋1单元
        String[] placeList = new String[]{"中华人民共和国吉林省长春市二道区临河街万兴小区4栋2门","中华人民共和国河北省石家庄市武林区砂石街河东小区8栋1单元"};
        for (String place : placeList) {
            addressResolution(place);
            System.out.println("-----------分割线-----------");
        }
    }

//    (?<town>[^小区]+小区|)
    /**
     * @Description: 利用正则表达式对地址进行解析
     * @Param:
     * @return:
     */
    public static void addressResolution(String address){
        String regex = "((?<country>[^国]+国|)(?<province>[^省]+省|.+自治区)|上海|北京|天津|重庆)(?<city>[^市]+市|.+自治州)(?<district>[^县]+县|.+区|.+镇|.+局)(?<street>[^街]+街|.+镇)(?<village>.*)";
        Matcher m = Pattern.compile(regex).matcher(address);
        String country=null,province=null,city=null,district=null,town=null,village=null,street=null;
        Map<String,String> row=null;
        while(m.find()){
            row=new LinkedHashMap<String,String>();
            country=m.group("country");
            System.out.println(country.trim());
            row.put("国家", country==null?"":country.trim());

            province=m.group("province");
            row.put("省份/直辖市", province==null?"":province.trim());
            System.out.println(province.trim());

            city=m.group("city");
            row.put("城市", city==null?"":city.trim());
            System.out.println(city.trim());

            district=m.group("district");
            row.put("区/县", district==null?"":district.trim());
            System.out.println(district.trim());

            street=m.group("street");
            row.put("街", street==null?"":street.trim());
            System.out.println(street.trim());

            town=m.group("town");
            row.put("小区", town==null?"":town.trim());
            System.out.println(town.trim());

            village=m.group("village");
            row.put("住址", village==null?"":village.trim());
            System.out.println(village.trim());
            System.out.println(row);
        }
    }
}
